package com.rhino.security.handler;

import com.alibaba.fastjson.JSON;
import com.rhino.security.constants.SecurityConstants;
import com.rhino.security.vo.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 退出成功处理器
 */
@Slf4j
@Component
public class JwtLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.debug("退出成功：", authentication);
        response.setStatus(HttpStatus.OK.value());
        response.setContentType(SecurityConstants.APPLICATION_JSON_UTF8_VALUE);
        PrintWriter writer = response.getWriter();

        //退出成功，返回前端，前端处理数据
        RestResult result=RestResult.OK("退出成功");
        writer.write(JSON.toJSONString(result));
        writer.close();
    }
}
